﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CopyFileList
{
    public partial class uGetFileList : UserControl
    {
        public uGetFileList()
        {
            InitializeComponent();
        }
        #region 定义
        ToolsCollection.BLL.TC_BAGLIST bll_Tc_BagList = new ToolsCollection.BLL.TC_BAGLIST();
        ToolsCollection.BLL.TC_FILES bll_Tc_Files = new ToolsCollection.BLL.TC_FILES();
        DataTable bagTB = new DataTable();
        /// <summary>
        /// 文件列表
        /// </summary>
        List<string> flie_Paths_List = new List<string>(); 
        #endregion
        private void dgv_Files_DragEnter(object sender, DragEventArgs e)
        {
            if (string.IsNullOrEmpty(this.txt_Path.Text))
            {
                MessageBox.Show("小样，先指定路径，再来拖，OK？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            var v = (string[])e.Data.GetData("FileDrop");
            foreach (var item in v)
            {
                if (!flie_Paths_List.Contains(item))
                {
                    
                    Get_File_List(item);
                }
            }
        }

        private void btn_ClearFiles_Click(object sender, EventArgs e)
        {
            this.flie_Paths_List.Clear();
            this.dgv_Files.Rows.Clear();
        }

        private void btn_CopyFiles_Click(object sender, EventArgs e)
        {
            #region 非空提示
            if (string.IsNullOrEmpty(this.txt_Path.Text))
            {
                MessageBox.Show("小样，没指定路径也想复制？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.dgv_Files.Rows.Count < 2)
            {
                MessageBox.Show("小样，一个文件都没有，让俺咋复制？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            #endregion
            this.btn_CopyFiles.Enabled = false;
            //任务包ID号
            int bagList_Id = Convert.ToInt32(this.cbb_Name.SelectedValue);
            //先删除所有文件列表
            bll_Tc_Files.Del_TC_Files(bagList_Id);
            //新增加文件列表
            List<string> add_File_List = new List<string>();
            //修改文件列表
            List<string> change_File_List = new List<string>();
            #region 添加到数据库中
            List<ToolsCollection.MODEL.TC_FILES> list_Tc_Files = new List<ToolsCollection.MODEL.TC_FILES>();
            flie_Paths_List.ForEach(lt =>
            {
                ToolsCollection.MODEL.TC_FILES tc_Files = new ToolsCollection.MODEL.TC_FILES();
                tc_Files.BagList_Id = bagList_Id;
                tc_Files.File_Path = lt;
                list_Tc_Files.Add(tc_Files);

                #region NEW
                //获取文件名
                string fileName = Path.GetFileName(lt);
                //获取目标路径
                //从配置文件里获取截取方法，是从IFOC开始，还是从哪个地方开始
                //IFOC位置
                int ifocIndex = lt.IndexOf("\\IFOC\\");
                int fileNameIndex = lt.IndexOf(fileName);

                string targetNewPath = string.Concat(this.txt_Path.Text, "\\new", lt.Substring(ifocIndex, fileNameIndex - ifocIndex));
                if (!Directory.Exists(targetNewPath.Replace(@"\\",@"\")))
                {
                    Directory.CreateDirectory(targetNewPath);
                }
                //拼接目标路径
                File.Copy(lt, targetNewPath + fileName, true);
                #endregion

                #region OLD
                //从配置文件中读取
                string old_File_Path = "\\\\192.168.0.17\\test\\" + lt.Substring(ifocIndex + 1, fileNameIndex - ifocIndex - 1);
                string c = (old_File_Path + fileName).Replace(@"\\", @"\");
                if (File.Exists((old_File_Path + fileName)))
                {
                    string targetOldPath = string.Concat(this.txt_Path.Text, @"\old", lt.Substring(ifocIndex, fileNameIndex - ifocIndex));
                    if (!Directory.Exists(targetOldPath))
                    {
                        Directory.CreateDirectory(targetOldPath);
                    }
                   change_File_List.Add(lt.Substring(ifocIndex, fileNameIndex - ifocIndex) + fileName);
                    //拼接目标路径
                    File.Copy(old_File_Path + fileName, targetOldPath + fileName, true);
                }
                else
                {
                    add_File_List.Add(lt.Substring(ifocIndex, fileNameIndex - ifocIndex) + fileName);
                }
                #endregion

            });
            bll_Tc_Files.Add_TC_Files(list_Tc_Files);
            #endregion

            this.btn_CopyFiles.Enabled = true;
            MessageBox.Show("应该OK了！这就帮亲打开文件夹！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            System.Diagnostics.Process.Start(this.txt_Path.Text);
            //弹出文件列表框
            FileList fl = new FileList();
            fl.Change_FileList = change_File_List;
            fl.Add_File_List = add_File_List;
            fl.ShowDialog();

        }

        private void txt_Name_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Enter)
            {
                bagTB = bll_Tc_BagList.GetTC_BagList(this.txt_Name.Text);
                this.cbb_Name.DataSource =bagTB ;
                this.cbb_Name.DisplayMember = "BAGLIST_NAME";
                this.cbb_Name.ValueMember = "BAGLIST_ID";
            }
        }

        private void btn_Load_Click(object sender, EventArgs e)
        {
           
            //如果存在就是目录
            //遍历得到文件清单
            //如果是文件，直接添加


        }

        private void cbb_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            var drr = bagTB.AsEnumerable().Where(lt => lt["baglist_name"].ToString() == this.cbb_Name.Text).ToList();
            if (drr.Count<1)
            {
                return;
            }
            this.txt_Path.Text = drr[0]["baglist_path"].ToString();
        }
        #region 递归取文件清单
        private void Get_File_List(string path)
        {
            //判断目录是否存在
            bool temp = Directory.Exists(path);
            if (temp)
            {
                string [] files = Directory.GetFileSystemEntries(path);
                foreach (var item in files)
                {
                    Get_File_List(item);
                }
            }
            else
            {
                if (!flie_Paths_List.Contains(path))
                {
                    this.dgv_Files.Rows[this.dgv_Files.Rows.Add()].Cells[0].Value = path;
                    flie_Paths_List.Add(path);
                }
            }
        }
        #endregion
    }
}
